<?php
namespace app\models;
use common\component\tool\Tool;
use app\models\Basic;

use Yii;

class User extends Basic
{
    
    public static function tableName()
    {
        return '{{%user}}';
    }

    public function rules()
    {
        return [
            ['app_id', 'required', 'message' => 'app_id不能为空', 'on' => 'add'],
            ['app_id', 'integer', 'message' => 'app_id必须为数字，或数字字符串', 
                        'max'=>18446744073709551615, 'tooBig' => 'app_id最大存储18446744073709551615'],
            ['userid', 'string', 'message' => 'userid必须是字符串',
                                'max' => 32, 'tooLong' => 'userid最长32位'],
            ['username', 'string', 'message' => 'username必须是字符串',
                                'max' => 32, 'tooLong' => 'username最长32位'],
            ['password', 'string', 'message' => 'password必须是字符串',
                                'min' => 32, 'tooShort' => 'password最短32位',
                                'max' => 32, 'tooLong' => 'password最长32位'],
            ['real_name', 'string', 'message' => 'real_name必须是字符串',
                                'max' => 8, 'tooLong' => 'real_name最长8位'],
            ['phone', 'string', 'message' => 'phone必须是字符串',
                                'min' => 11, 'tooShort' => 'phone最短11位',
                                'max' => 11, 'tooLong' => 'phone最长11位'],
            ['nickname', 'string', 'message' => 'nickname必须是字符串',
                                'max' => 32, 'tooLong' => 'nickname最长32位'],
            ['openid', 'string', 'message' => 'openid必须是字符串',
                                'max' => 64, 'tooLong' => 'openid最长64位'],
            ['sex', 'integer', 'message' => 'sex必须为数字，或数字字符串'],
            ['sex', 'in', 'range' => array(0, 1, 2), 'message' => 'sex值必须在0~2中选择'], 
            ['headimgurl', 'string', 'message' => 'headimgurl必须是字符串',
                                'max' => 512, 'tooLong' => 'headimgurl最长512位'],
            ['identity', 'integer', 'message' => 'identity必须为数字，或数字字符串'],
            ['identity', 'in', 'range' => array(1, 2), 'message' => 'identity值必须在1~2中选择'], 
            ['description', 'string', 'message' => 'description必须是字符串',
                                'max' => 512, 'tooLong' => 'description最长512位'],
            ['reg_client', 'string', 'message' => 'reg_client必须是字符串',
                                'max' => 1024, 'tooLong' => 'reg_client最长1024位'],
            ['lastloginip', 'string', 'message' => 'lastloginip必须是字符串',
                                'max' => 64, 'tooLong' => 'lastloginip最长64位'],
            ['lastlogin_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['lastlogin_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'lastlogin_time非yyyy-MM-dd HH:mm:ss格式'],
            ['visitor_num', 'integer', 'message' => 'visitor_num必须为数字，或数字字符串', 
                        'max'=>18446744073709551615, 'tooBig' => 'visitor_num最大存储18446744073709551615'],
            ['onlinetimes', 'integer', 'message' => 'onlinetimes必须为数字，或数字字符串', 
                        'max'=>18446744073709551615, 'tooBig' => 'onlinetimes最大存储18446744073709551615'],
            ['responsetimes', 'integer', 'message' => 'responsetimes必须为数字，或数字字符串', 
                        'max'=>18446744073709551615, 'tooBig' => 'responsetimes最大存储18446744073709551615'],
            ['logintimes', 'integer', 'message' => 'logintimes必须为数字，或数字字符串', 
                        'max'=>18446744073709551615, 'tooBig' => 'logintimes最大存储18446744073709551615'],
            ['state', 'integer', 'message' => 'state必须为数字，或数字字符串'],
            ['state', 'in', 'range' => array(1, 2), 'message' => 'state值必须在1,2中选择'],
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }


    /*添加*/
    public function addUser($data, $app_id)
    {
        // P($data);
        $this->scenario = 'add';
        $data['User']['app_id'] = $app_id;
        if(isset($data['User']['state'])){
            $data['User']['state'] = Tool::changeBool($data['User']['state']);
        }
        // P($data);
        if($this->load($data) and $this->validate()){
            // P($this);
            if($this->save(false)){
                $id = $this->getPrimaryKey();
                return $id;
            }
        }
        return false;
    }

    /*
    修改
    $data       修改的数据
    $app_id 
    */
    public function modifyFaq($data, $id)
    {
        // P($data);
        $faq = self::find()->where('id = :id', [':id' => $id])->one();
        // P($faq);
        if(isset($data['User']['state'])){
            $data['User']['state'] = Tool::changeBool($data['User']['state']);
        }
        // P($data);
        if(!is_null($faq)){
            if($this->load($data) and $this->validate()){
                if(isset($this->username)){
                    $faq->username = $this->username;
                }
                if(isset($this->password)){
                    $faq->password = $this->password;
                }
                if(isset($this->real_name)){
                    $faq->real_name = $this->real_name;
                }
                if(isset($this->phone)){
                    $faq->phone = $this->phone;
                }
                if(isset($this->headimgurl)){
                    $faq->headimgurl = $this->headimgurl;
                }
                if(isset($this->description)){
                    $faq->description = $this->description;
                }
                if(isset($this->state)){
                    $faq->state = $this->state;
                }
                if($faq->save(false)){
                    return true;
                }
            }
        };
        return false;
    }

    
}